package org.springforge.forgeweb.mvc;


import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.naming.NamingException;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import org.springforge.ldap.service.LdapService;


@Controller
public class LDAPController {
    
	@Autowired
	protected LdapService ldapService = null;
	
    @RequestMapping(value="/user/index.html")
    public void index(HttpSession session, Model model){
    	String username = (String) session.getAttribute("user");
    	try{
    	model.addAttribute(	"firstname",
    						ldapService.getFirstName(username));
    	model.addAttribute(	"lastname",  	
    						ldapService.getLastName(username));
    	model.addAttribute(	"email",  	
    						username);
    	}catch(NamingException e){e.printStackTrace();}
    }
    
    
    @RequestMapping(value="/user/profil.html")
    public void profil(	@RequestParam(value="firstname",required=false) String firstname,
						@RequestParam(value="surname",required=false) String surname,
						@RequestParam(value="email",required=false) String email,
						@RequestParam(value="pwd",required=false) String password,
						@RequestParam(value="pwd2",required=false) String password2,
						HttpSession session,Model model){
    	String username=(String) session.getAttribute("user");
    	if(firstname != null && surname != null && email != null )
    	{
    		if( (password.isEmpty() && password2.isEmpty() ) || (password.equals(password2)) )
    		{
    			try {
					ldapService.update(username, email, password, firstname, surname);
					session.setAttribute("user", email);
					username = email;
				} catch (NamingException e) {e.printStackTrace();}
				catch(UnsupportedEncodingException e){e.printStackTrace();}
    			catch(NoSuchAlgorithmException e){e.printStackTrace();}
    		}
    		else
    		{
    			model.addAttribute("error", "Votre confirmation de mot de passe " +
    										"et votre mot de passe sont different");
    		}
    	}
    	try{
        	model.addAttribute(	"firstname",
        						ldapService.getFirstName(username));
        	model.addAttribute(	"lastname",  	
        						ldapService.getLastName(username));
        	model.addAttribute(	"email",username);
        	}catch(NamingException e){e.printStackTrace();}
    }
   
    @RequestMapping(value="/user/create.html")
    public ModelAndView create(	@RequestParam(value="firstname",required=false) String firstname,
    					@RequestParam(value="surname",required=false) String surname,
    					@RequestParam(value="email",required=false) String email,
    					@RequestParam(value="pwd",required=false) String password,
    					@RequestParam(value="pwd2",required=false) String password2,
    					Model model){
    	String error = "";
    	if(firstname!=null && surname!=null && email!=null && password !=null && password2 !=null){
    		try {
    			if(firstname != null && surname != null && email != null && 
    					password != null && password2 != null){
    				if(password.equals(password2)){
    				ldapService.create(email, password, firstname, surname);
    			}else{
    				error = "Le mot de passe de confirmation est different du mot de passe souhaite";
    				}
    			}
			} catch (NamingException e) {
				e.printStackTrace();
			}
    	}
    	else{
    		error = "Veuillez remplir correctement le formulaire svp";
    	}
    	ModelAndView ret =  new ModelAndView("redirect:/");
    	if(error.isEmpty())	return ret;
    	ret.getModel().put("error", error);
    	return ret;
    }
    
    
    @RequestMapping(value="/user/login.html")
    public ModelAndView login(@RequestParam(value="email",required=false) String email,
							  @RequestParam(value="pwd",required=false) String password,
							  HttpSession session){
    	if(ldapService.authenticate(email, password)){
	    	session.setAttribute("user", email);
	    	return new ModelAndView("redirect:/user/index.html");
	    }
	    ModelAndView accueil = new ModelAndView("redirect:/");
	    accueil.getModel().put("error","Utilisateurs non reconnus");
	    return accueil;
    }
    
    @RequestMapping(value="/user/logout.html",method=RequestMethod.GET)
    public ModelAndView logout(HttpSession session){
    	session.removeAttribute("user");
    	session.invalidate();
	    return new ModelAndView("redirect:/");
    }
    
    @RequestMapping(value="/user/remove.html")
    public ModelAndView remove(HttpSession session){
    	String username = (String)session.getAttribute("user");
    	try {
			ldapService.delete(username);
			session.removeAttribute("user");
			session.invalidate();
			return new ModelAndView("redirect:/");
		} catch (NamingException e) {
			e.printStackTrace();
		}
    	return new ModelAndView("redirect: /forge/user/index.html");
    }
}